package cn.edu.scau.cmi.ema.util;

import org.apache.commons.math3.fitting.PolynomialCurveFitter;
import org.apache.commons.math3.fitting.WeightedObservedPoints;

import java.util.List;

public class CurverFitUtil {//	预测下一个结果

	public static Long predictNextByPolynomialCurveFitter(List<Long> x, List<Long> y) {
		int size = y.size() > 10 ? 10 : y.size();
		int degree = size - 1;

		long startTime = System.currentTimeMillis();

		WeightedObservedPoints points = new WeightedObservedPoints();
		for (int i = y.size() - size, j = 1; i < y.size(); i++, j++) {
//			points.add(x.get(i), y.get(i));
			// 在这里添加输入数据和权重（j为权重）
//			目前用等差数列的方式，可以任意数组

			points.add(j, x.get(i), y.get(i));
		}
		PolynomialCurveFitter fitter = PolynomialCurveFitter.create(degree);
		double[] result = fitter.fit(points.toList());
		long endTime = System.currentTimeMillis();
//		System.out.println(result.length);
		double d = 0;
		for (int deg = 0; deg < result.length; deg++) {
			d += Math.pow(x.get(size - 1) + 1, deg) * result[deg];
		}
		System.out.print(d + " ");
//		for (double i : result) {
//			System.out.print(i + " ");
//		}
//		System.out.println("\n结果为：" + d);
//		System.out.println("\n使用时间" + (endTime - startTime));
		return d > 0 ? (long) d : 0;
	}


}
